#include "stdafx.h"
#include "CoordSys.h"
#include "CoordConvert.h"

int			CoordSys::m_zoom = 1;
double		CoordSys::m_eCenterX = 0.0f;
double		CoordSys::m_eCenterY = 0.0f;
int			CoordSys::m_cLeft	= 0;
int			CoordSys::m_cRight	= 0;
int			CoordSys::m_cTop	= 0;
int			CoordSys::m_cBottom = 0;

void CoordSys::CleanStatus()
{
	m_zoom = 1;
	m_eCenterX = 0.0f;
	m_eCenterY = 0.0f;
	//m_cLeft	= 0;
	//m_cRight	= 0;
	//m_cTop	= 0;
	//m_cBottom = 0;
}

void CoordSys::Earth2Screen(double ex, double ey, int& cx, int& cy)
{
	double dRCX = (m_cLeft +  m_cRight) / 2;
	double dRCY = (m_cTop + m_cBottom) / 2;

	double dX = ex - m_eCenterX;
	double dY = -(ey - m_eCenterY);
	cx = int(dRCX + dX / m_zoom);
	cy = int(dRCY + dY / m_zoom);
}

void CoordSys::SetZoomFactor(int zoom)
{
	m_zoom = zoom;
}

void CoordSys::SetEarthCenter(double ex, double ey)
{
	m_eCenterX = ex;
	m_eCenterY = ey;
}

void CoordSys::SetWindowRect(int left, int right, int top, int bottom)
{
	m_cLeft = left;
	m_cRight = right;
	m_cTop = top;
	m_cBottom = bottom;
}

bool CoordSys::InRect(double ex, double ey)
{
	int x, y;
	Earth2Screen(ex, ey, x, y);

	if (x < m_cLeft || x > m_cRight || y < m_cTop || y > m_cBottom)
		return false;

	return true;
}

void CoordSys::Longlat2UTM(double longitude, double latitude, double &x, double &y)
{ 
	// if the coordinates are longtitude/latitude
	wchar_t szUTM[255];
	LonLat2UTM::convertLonLatToUTM(longitude, latitude, &x, &y, szUTM);	
}
